package com.warm.pump.module.sys.controller;

import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.shiro.ShiroUtils;
import com.warm.pump.frame.util.PageUtils;
import com.warm.pump.module.sys.bean.SysUserDto;
import com.warm.pump.module.sys.bean.po.gen.SysUser;
import com.warm.pump.module.sys.service.ISysUserRoleService;
import com.warm.pump.module.sys.service.ISysUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:user:list")
    public OutJson list(Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("offset", (page - 1) * limit);
        map.put("limit", limit);

        // 查询列表数据
        List<SysUser> userList = sysUserService.queryList(map);
        int total = sysUserService.queryTotal(map);

        PageUtils pageUtil = new PageUtils(userList, total, limit, page);

        return OutJson.ok().put("page", pageUtil);
    }

    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/info")
    public OutJson info() {
        return OutJson.ok().put("user", getUser());
    }

    /**
     * 修改登录用户密码
     */
    @RequestMapping("/password")
    public OutJson password(String password, String newPassword) {
        if (StringUtils.isBlank(newPassword)) {
            return OutJson.error("新密码不为能空");
        }

        // sha256加密
        password = new Sha256Hash(password).toHex();
        // sha256加密
        newPassword = new Sha256Hash(newPassword).toHex();

        // 更新密码
        int count = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (count == 0) {
            return OutJson.error("原密码不正确");
        }

        // 退出
        ShiroUtils.logout();

        return OutJson.ok();
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public OutJson info(@PathVariable("userId") Long userId) {
        SysUserDto user = new SysUserDto();
        SysUser sysUser = sysUserService.queryObject(userId);
        try {
            MyBeanUtils.copyBean2Bean(user, sysUser);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return OutJson.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:user:save")
    public OutJson save(@RequestBody SysUserDto user) {
        if (StringUtils.isBlank(user.getUsername())) {
            return OutJson.error("用户名不能为空");
        }
        if (StringUtils.isBlank(user.getPassword())) {
            return OutJson.error("密码不能为空");
        }

        sysUserService.save(user);

        return OutJson.ok();
    }

    /**
     * 修改用户
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:user:update")
    public OutJson update(@RequestBody SysUserDto user) {
        if (StringUtils.isBlank(user.getUsername())) {
            return OutJson.error("用户名不能为空");
        }

        sysUserService.update(user);

        return OutJson.ok();
    }

    /**
     * 删除用户
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public OutJson delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return OutJson.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return OutJson.error("当前用户不能删除");
        }

        sysUserService.deleteBatch(userIds);

        return OutJson.ok();
    }
}
